const express = require('express');
const auth = require('../../middleware/auth');

const router = express.Router();

// 获取仪表板统计数据
router.get('/stats', auth, async (req, res) => {
  try {
    // 模拟统计数据
    const stats = {
      devices: {
        total: 156,
        online: 142,
        offline: 14,
        warning: 3
      },
      energy: {
        todayConsumption: 2847.5,
        monthlyConsumption: 85420.3,
        efficiency: 92.5,
        cost: 12847.2
      },
      parking: {
        total: 500,
        occupied: 387,
        available: 113,
        revenue: 15420.5
      },
      security: {
        alerts: 2,
        incidents: 0,
        cameras: 24,
        accessPoints: 12
      },
      personnel: {
        total: 45,
        present: 38,
        absent: 7,
        visitors: 12
      }
    };

    res.json(stats);
  } catch (error) {
    console.error('Dashboard stats error:', error);
    res.status(500).json({ error: '获取统计数据失败' });
  }
});

// 获取实时数据
router.get('/realtime', auth, async (req, res) => {
  try {
    const realtimeData = {
      timestamp: new Date().toISOString(),
      deviceStatus: {
        online: Math.floor(Math.random() * 100) + 50,
        offline: Math.floor(Math.random() * 20) + 5,
        warning: Math.floor(Math.random() * 10) + 1
      },
      energyConsumption: {
        current: Math.floor(Math.random() * 1000) + 500,
        today: Math.floor(Math.random() * 10000) + 5000,
        thisMonth: Math.floor(Math.random() * 100000) + 50000
      },
      parkingStatus: {
        total: 500,
        occupied: Math.floor(Math.random() * 400) + 100,
        available: Math.floor(Math.random() * 100) + 50
      },
      securityAlerts: Math.floor(Math.random() * 5)
    };

    res.json(realtimeData);
  } catch (error) {
    console.error('Realtime data error:', error);
    res.status(500).json({ error: '获取实时数据失败' });
  }
});

module.exports = router;
